package com.epro.standard.usermenu.manage;

import java.util.Vector;

import com.epro.standard.StandardException;
import com.epro.standard.infrastructure.dataaccess.DBManage;
import com.epro.standard.infrastructure.master.MasterManage;
import com.epro.standard.usermenu.RoleMenuEntity;
import com.epro.standard.usermenu.RoleMenuKeyword;
import com.epro.standard.usermenu.RoleMenuSortKeyword;
import com.epro.standard.usermenu.RoleMenuVO;
import com.epro.standard.usermenu.RoleMenu_MenuKeyword;

public class MenuRoleManage {
    private DBManage dbm = new DBManage();

    /**
   @roseuid 3EA8E7A600E4
     */
    public MenuRoleManage() {

    }

    private void manager(int dbType, 
                         RoleMenuEntity roleMenuEntity) throws StandardException {
        MasterManage ct = new MasterManage();
        switch (dbType) {
        case DBManage.INSERT:
            ct.insert(roleMenuEntity);
            break;
        case DBManage.UPDATE:
            ct.update(roleMenuEntity);
            break;
        case DBManage.DELETE:
            ct.delete(roleMenuEntity);
            break;
        case DBManage.INSERT_ID_GENERATOR:
            ct.insertIDGenerator(roleMenuEntity);
            break;
        }
    }

    /**
   @param roleMenuEntity
   @roseuid 3EA8DF3B01CF
     */
    public void addMenuByRole(RoleMenuEntity roleMenuEntity) throws StandardException {

        manager(DBManage.INSERT, roleMenuEntity);
    }

    /**
   @param roleMenuEntity
   @roseuid 3EA8DF3B01D1
     */
    public void editMenuByRole(RoleMenuEntity roleMenuEntity) throws StandardException {
        manager(DBManage.UPDATE, roleMenuEntity);
    }

    /**
   @param roleMenuEntity
   @roseuid 3EA8DF3B01D3
     */
    public void removeMenuByRole(RoleMenuEntity roleMenuEntity) throws StandardException {
        manager(DBManage.DELETE, roleMenuEntity);
    }


    private Vector sort(String roleID, 
                        RoleMenu_MenuKeyword roleMenu_MenuKeyword, 
                        RoleMenuSortKeyword roleMenuSortKeyword, 
                        boolean isAsc) throws StandardException {
        if (roleMenuSortKeyword.isMenuID()) {
            if (isAsc)
                roleMenu_MenuKeyword.addOrderAsc(roleMenu_MenuKeyword.getRM_menuID());
            else
                roleMenu_MenuKeyword.addOrderDesc(roleMenu_MenuKeyword.getRM_menuID());
        } else if (roleMenuSortKeyword.isMenuName()) {
            if (isAsc)
                roleMenu_MenuKeyword.addOrderAsc(roleMenu_MenuKeyword.getM_menuName());
            else
                roleMenu_MenuKeyword.addOrderDesc(roleMenu_MenuKeyword.getM_menuName());
        } else if (roleMenuSortKeyword.isAbleExecute()) {
            if (isAsc)
                roleMenu_MenuKeyword.addOrderAsc(roleMenu_MenuKeyword.getRM_ableExecute());
            else
                roleMenu_MenuKeyword.addOrderDesc(roleMenu_MenuKeyword.getRM_ableExecute());
        } else if (roleMenuSortKeyword.isAbleInsert()) {
            if (isAsc)
                roleMenu_MenuKeyword.addOrderAsc(roleMenu_MenuKeyword.getRM_ableInsert());
            else
                roleMenu_MenuKeyword.addOrderDesc(roleMenu_MenuKeyword.getRM_ableInsert());
        } else if (roleMenuSortKeyword.isAbleUpdate()) {
            if (isAsc)
                roleMenu_MenuKeyword.addOrderAsc(roleMenu_MenuKeyword.getRM_ableUpdate());
            else
                roleMenu_MenuKeyword.addOrderDesc(roleMenu_MenuKeyword.getRM_ableUpdate());
        } else if (roleMenuSortKeyword.isAbleDelete()) {
            if (isAsc)
                roleMenu_MenuKeyword.addOrderAsc(roleMenu_MenuKeyword.getRM_ableDelete());
            else
                roleMenu_MenuKeyword.addOrderDesc(roleMenu_MenuKeyword.getRM_ableDelete());
        }


        return dbm.executeQuery(roleMenu_MenuKeyword);
    }

    /**
   	@param roleID
   	@return java.util.Vector
   	@roseuid 3EA8DF3B01D5
     */
    public Vector getMenuByRole(String roleID) throws StandardException {
        RoleMenuKeyword roleMenuKeyword = new RoleMenuKeyword();
        roleMenuKeyword.addCommonCriteria(roleMenuKeyword.getRM_roleID(), 
                                          roleID.trim());
        roleMenuKeyword.addOrderAsc(roleMenuKeyword.getRM_menuID());

        return dbm.executeQuery(roleMenuKeyword);
    }

    public Vector getMenuByRole(String roleID, 
                                RoleMenuSortKeyword roleMenuSortKeyword, 
                                boolean isAsc) throws StandardException {
        RoleMenu_MenuKeyword roleMenu_MenuKeyword = new RoleMenu_MenuKeyword();
        roleMenu_MenuKeyword.addCommonCriteria(roleMenu_MenuKeyword.getRM_roleID(), 
                                               roleID.trim());
        roleMenu_MenuKeyword.setJoin(true, true);
        return sort(roleID, roleMenu_MenuKeyword, roleMenuSortKeyword, isAsc);
    }

    public Vector getMenuByRoleWhichExecute(String roleID) throws StandardException {
        RoleMenuKeyword roleMenuKeyword = new RoleMenuKeyword();
        roleMenuKeyword.addCommonCriteria(roleMenuKeyword.getRM_roleID(), 
                                          roleID.trim());
        roleMenuKeyword.addCommonCriteria(roleMenuKeyword.getRM_ableExecute(), 
                                          "Y");
        roleMenuKeyword.addOrderAsc(roleMenuKeyword.getRM_menuID());

        return dbm.executeQuery(roleMenuKeyword);
    }

    public Vector getMenuByRoleWhichExecute(String roleID, 
                                            RoleMenuSortKeyword roleMenuSortKeyword, 
                                            boolean isAsc) throws StandardException {
        RoleMenu_MenuKeyword roleMenu_MenuKeyword = new RoleMenu_MenuKeyword();
        roleMenu_MenuKeyword.addCommonCriteria(roleMenu_MenuKeyword.getRM_roleID(), 
                                               roleID.trim());
        roleMenu_MenuKeyword.addCommonCriteria(roleMenu_MenuKeyword.getRM_ableExecute(), 
                                               "Y");
        roleMenu_MenuKeyword.setJoin(true, true);
        return sort(roleID, roleMenu_MenuKeyword, roleMenuSortKeyword, isAsc);
    }

    public RoleMenuVO getMenu(String roleID, 
                              String menuID) throws StandardException {
        RoleMenuKeyword roleMenuKeyword = new RoleMenuKeyword();
        roleMenuKeyword.addCommonCriteria(roleMenuKeyword.getRM_roleID(), 
                                          roleID.trim());
        roleMenuKeyword.addCommonCriteria(roleMenuKeyword.getRM_menuID(), 
                                          menuID.trim());
        roleMenuKeyword.addOrderAsc(roleMenuKeyword.getRM_menuID());

        Vector v = dbm.executeQuery(roleMenuKeyword);
        return (RoleMenuVO)v.elementAt(0);
    }
}
